#include "Network/PackageReader.h"

namespace PocketSurvival
{
    void PackageReader::readPoint2(Point2 &point)
    {
        point.posX = readInt32();
        point.posY = readInt32();
    }
    void PackageReader::readPoint3(Point3 &point3)
    {
        point3.posX = readInt32();
        point3.posY = readInt32();
        point3.posZ = readInt32();
    }
    void PackageReader::readBlockPoint3(Point3 &point)
    {
        point.posX = readInt32();
        point.posY = readUInt8();
        point.posZ = readInt32();
    }

    void PackageReader::readVector2(Vector2 &vector)
    {
        vector.posX = readFloat();
        vector.posY = readFloat();
    }
    void PackageReader::readVector3(Vector3 &vector3)
    {
        vector3.posX = readFloat();
        vector3.posY = readFloat();
        vector3.posZ = readFloat();
    }
    void PackageReader::readQuaternion(Quaternion &quaternion)
    {
        quaternion.X = readFloat();
        quaternion.Y = readFloat();
        quaternion.Z = readFloat();
        quaternion.W = readFloat();
    }
    void PackageReader::readRay3(Ray3 &ray3)
    {
        readVector3(ray3.Position);
        readVector3(ray3.Direction);
    }
    void PackageReader::readCellFace(CellFace &cellFace)
    {
        readBlockPoint3(cellFace.point);
        cellFace.face = readUInt8();
    }
    void PackageReader::readColor4(Color4 &color)
    {
        color.R = readUInt8();
        color.G = readUInt8();
        color.B = readUInt8();
        color.A = readUInt8();
    }
    void PackageReader::readTerrainRaycastResult(TerrainRaycastResult &terrainRaycastResult)
    {
        readRay3(terrainRaycastResult.ray);
        terrainRaycastResult.value = readInt32();
        terrainRaycastResult.collisionBoxIndex = readInt32();
        terrainRaycastResult.distance = readFloat();
        readCellFace(terrainRaycastResult.cellFace);
    }
    void PackageReader::readMatrix(Matrix &matrix)
    {
        // uint8_t matrixSize = readUInt8();
        // read((char*)&matrix, matrixSize);
        matrix.M11 = readFloat();
        matrix.M21 = readFloat();
        matrix.M31 = readFloat();
        matrix.M41 = readFloat();

        matrix.M12 = readFloat();
        matrix.M22 = readFloat();
        matrix.M32 = readFloat();
        matrix.M42 = readFloat();

        matrix.M13 = readFloat();
        matrix.M23 = readFloat();
        matrix.M33 = readFloat();
        matrix.M43 = readFloat();

        matrix.M14 = readFloat();
        matrix.M24 = readFloat();
        matrix.M34 = readFloat();
        matrix.M44 = readFloat();
    }

    void PackageReader::readGuid(GameGuid &gameGuid)
    {
        uint8_t len = readUInt8();
        if(len != sizeof(GameGuid))
        {
            throw std::runtime_error(fmt::format("读取的guid长度不相符,读取长度: {}, 需要的长度: {}", len, sizeof(GameGuid)).c_str());
        }
        read(gameGuid.data(), sizeof(GameGuid));
    }

} // namespace PocketSurvival
